package org.xussen.dic;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MakeArray {

	private static String charEncoding = "UTF-8";
	/**
	 * 词库地址
	 */
	private static String path = "data/library1.dic";

	public static void main(String[] args) throws Exception {
		long start = System.currentTimeMillis();
		BufferedReader reader = IOUtil.getReader(path, charEncoding);
		//磁盘读词库文件
		Set<String> keyWords = getLibrary(reader);
		//将词库里面的所有词构造成树
		Node node = TreeUtil.addLibrary(keyWords);
		List<Node> head = new ArrayList<Node>();
		head.add(node);
		//遍历树，获取排好序的节点列表
		List<Node> all = TreeUtil.treeToLibrary(head, 0, 1);
		all.remove(0) ;
		/**
		 * 开始构造array
		 */
		Dictionary.makeArray(all);
		System.out.println(System.currentTimeMillis()-start);
		System.out.println();
	}

	public static Set<String> getLibrary(BufferedReader reader) throws Exception {
		String line;
		Set<String> keyWords = new HashSet<String>();
		while ((line = reader.readLine()) != null) {
			if (line.contains("#")) {
				continue;
			}
			keyWords.add(line);
		}
		return keyWords;
	}
}
